﻿ImagePreview = function(container, width, height, options)
{
    this.container = $(container);
    if(!this.container) throw new Error('容器控件' + container + '未找到.');
    
    this.width = width || this.container.offsetWidth;
    this.height = height || this.container.offsetHeight;
	this.src = '';
	options = options || {};
	
	this.checkSize = function(offset)
	{
		offset = offset || options.offset || 0;
		return  ! (this.size[0] > this.width * (1 + offset)
				|| this.size[1] > this.height * (1 + offset)
				|| this.size[0] < this.width * (1 - offset)
				|| this.size[1] < this.height * (1 - offset));
	}

	this.setSize = function(width, height)
	{
		if(arguments.length == 1)
			if(Is.ARRAY(width) || ((width = String(width).match(/(\d+)[\,\*](\d+)/)) && width.shift())) { height = width[1]; width = width[0] }
		
		if(width && height)
		{
			this.width = width;
			this.height = height;
			this.container.style.width = width + 'px';
			this.container.style.height = height + 'px';
			return true;
		}
		return false;
	}

    this.setImage = function(src)
	{
		if (!src) src = this.src;
        if (!src) return !!this.container.set('');
        this.src = src = src.replace(/ /g, '%20').replace(/\(/g, '%28').replace(/\)/g, '%29');
        if (Browser.msie) {
            var div = $(document.createElement('div')).css('cssText', 'width:1px;height:1px;position:absolute');
            this.container.innerHTML = '';
            this.container.appendChild(div);
            div.style.filter = "progid:dximagetransform.microsoft.alphaimageloader(sizingmethod=image)";
            div.filters.item(0).src = src;
            this.adjust(div);
            div.filters.item(0).sizingMethod = 'scale';
        } else {
            var image = new Image, me = this;
            image.src = src;
            image.onload = function() { me.adjust.call(me, this) }
            this.container.innerHTML = '';
            this.container.appendChild(image);
        }
    }

    this.onChange = function(sender)
	{
        if (!/.+\.(?:jpg|jpeg|gif|png|bmp)$/i.test(sender.value)) return false;
		if(options.beforePreview) if(options.beforePreview.call(this) === false) return false;
        sender.select();
        this.setImage(Browser.msie ? document.selection.createRange().text : sender.files[0].getAsDataURL());
		if(options.afterPreview) options.afterPreview.call(this);
		try{document.selection.empty()}catch(ex){sender.blur()}
    }

    this.adjust = function(e)
	{
        var width = e.offsetWidth, height = e.offsetHeight;
		this.size = [0, 0, width, height];
        if (width > this.width) (height = height * this.width / width) && (width = this.width);
        if (height > this.height) (width = width * this.height / height) && (height = this.height);
        e.style.width = (this.size[0] = width = parseInt(width)) + 'px';
        e.style.height = (this.size[1] = height = parseInt(height)) + 'px';
        if (width < this.width) e.style.marginLeft = parseInt((this.width - width) * .5) + 'px';
        if (height < this.height) e.style.marginTop = parseInt((this.height - height) * .5) + 'px';
    }
	
	if(options.input)
	{
		var me = this, input = $(options.input);
		if(input) input.onchange = function() { me.onChange(this) }
		else throw new Error('文件浏览控件' + options.input +'未找到');
	}
	
	this.reset = function()
	{
		var input = $(options.input), me = this;
		input.outerHTML = input.outerHTML.replace(/ value=\"[^\"]*\" /, ' ');
		$(options.input).onchange = function() { me.onChange(this) }
		this.container.html('');
		this.src = '';
	}
}
